﻿package mlc.haxe.output;
import haxe.Firebug;
import haxe.PosInfos;

/**
 * An output handler that passes all the information to Firebug.
 */
class FirebugOutputHandler implements ConsoleOutputHandler {
	private var target:ConsoleOutputHandler;
	/**
	 * Creates a new Firebug output handler.
	 */
	public function new():Void {
		target =
			// Isn't "detect" the worst name ever? Use an actual Firebug target if Firebug is available. If not, use a null output
			// handler.
			untyped if (Firebug.detect()) {
				new InternalFirebugOutputHandler();
			} else {
				new NullOutputHandler();
			}
	}
	public function clearOutput():Void {
		target.clearOutput();
	}
	public function writeAdvancedString(value:String, color:Int, breakline:Bool):Void {
		target.writeAdvancedString(value, color, breakline);
	}
	public function writeBool(value:Bool, positionInformation:PosInfos):Void {
		target.writeBool(value, positionInformation);
	}
	public function writeFloat(value:Float, positionInformation:PosInfos):Void {
		target.writeFloat(value, positionInformation);
	}
	public function writeInt(value:Int, positionInformation:PosInfos):Void {
		target.writeInt(value, positionInformation);
	}
	public function writeNull(positionInformation:PosInfos):Void {
		target.writeNull(positionInformation);
	}
	public function writeString(value:String, positionInformation:PosInfos):Void {
		target.writeString(value, positionInformation);
	}
	public function writeXml(value:Xml, positionInformation:PosInfos):Void {
		target.writeXml(value, positionInformation);
	}
}
class InternalFirebugOutputHandler implements ConsoleOutputHandler {
	public function new():Void {
	}
	public function clearOutput():Void {
	}
	public function writeAdvancedString(value:String, color:Int, breakline:Bool):Void {
		var positionInformation:PosInfos = here;
		positionInformation.fileName = "Firebug output handler";
		Firebug.trace(value, positionInformation);
	}
	public function writeBool(value:Bool, positionInformation:PosInfos):Void {
		Firebug.trace(value, positionInformation);
	}
	public function writeFloat(value:Float, positionInformation:PosInfos):Void {
		Firebug.trace(value, positionInformation);
	}
	public function writeInt(value:Int, positionInformation:PosInfos):Void {
		Firebug.trace(value, positionInformation);
	}
	public function writeNull(positionInformation:PosInfos):Void {
		Firebug.trace(null, positionInformation);
	}
	public function writeString(value:String, positionInformation:PosInfos):Void {
		Firebug.trace(value, positionInformation);
	}
	public function writeXml(value:Xml, positionInformation:PosInfos):Void {
		Firebug.trace(value, positionInformation);
	}
}